package gemini.pollux.ui.client.page.content.product.producttype.edit;

import gemini.basic.model.AbstractDomain;
import gemini.basic.model.ProductType;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class ProductTypeEditView extends Composite implements
		ProductTypeEditPresenter.Display {

	@UiTemplate("ProductTypeEditView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, ProductTypeEditView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	public ProductTypeEditView() {
		initWidget(binder.createAndBindUi(this));
	}

	@UiField
	HTML htmTitle;

	@UiField
	HTML htmInformation;

	@UiField
	TextBox tbxName;
	
	@UiField
	TextBox tbxCode;

	@UiField
	TextBox tbxImage;

	@UiField
	TextArea tarDescription;

	@UiField
	Button btnSubmit;

	@UiField
	Button btnBack;

	@UiField
	Button btnReset;

	@UiHandler("btnReset")
	void resetFields(ClickEvent event) {
		loadForm();
	}

	@Override
	public void initCustom() {

		tarDescription.setSize("300px", "200px");
		tbxName.setWidth("300px");
		tbxImage.setWidth("300px");
		tbxCode.setWidth("300px");
		
		tbxCode.setMaxLength(3);
		tbxImage.setMaxLength(50);
		tbxName.setMaxLength(20);

		if (productType == null) {
			htmTitle.setHTML("<h3>New Product Type</h3>");
			htmInformation
					.setHTML("Fill bellow information to create new product type");
		} else {
			htmTitle.setHTML("<h3>Update Product Type</h3>");
			htmInformation.setHTML("Update bellow information to update product type");
		}

		loadForm();
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	private ProductType productType;

	@Override
	public HasClickHandlers getBackClickHandlers() {
		return btnBack;
	}

	@Override
	public HasClickHandlers getSubmitClickHandlers() {
		return btnSubmit;
	}

	@Override
	public void setProductType(ProductType productType) {
		this.productType = productType;
	}

	public void loadForm() {
		if (this.productType == null) {
			tbxName.setValue("");
			tbxCode.setValue("");
			tbxImage.setValue("images/category/no_image.png");
			tarDescription.setValue("");
		} else {
			tbxName.setValue(this.productType.getName());
			tbxCode.setValue(this.productType
					.getCode());
			tbxImage.setValue(this.productType.getImageUrl());
			tarDescription.setValue(this.productType.getDescription());
		}
	}

	@Override
	public ProductType getProductType() {
		if (productType == null) {
			productType = new ProductType();
			productType.setId(AbstractDomain.TRANSIENT_ENTITY_ID);
		}

		String desc = tarDescription.getValue();

		if(desc != null && desc.length() > 300){
			desc.substring(0, 299);
		}
		
		productType.setDescription(desc);
		productType.setName(tbxName.getValue());
		productType.setCode(tbxCode.getValue());
		productType.setImageUrl(tbxImage.getValue());

		return productType;
	}
}
